﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharitiesLib;

namespace Charities
{
    public partial class SupplyAdd : Form
    {
        int cartd;
        int detailId;
        Form fo = null;
        public bool is_loaded = false;
        Form parent = null;
        public SupplyAdd(Form m )
        {
   
            fo = m;
            this.MdiParent = m;
            InitializeComponent();
        }
        public SupplyAdd(int ci,int cdi, Form mdi,Form _parent)
        {
            fo = mdi;
            parent = _parent;
            this.MdiParent = mdi;
            cartd = ci;
            detailId = cdi;
            InitializeComponent();
        }
        ModelContainer ctx = new ModelContainer();
        public delegate void UpdateFormHandler(object sender, UpdateFormEventArg e);
        public event UpdateFormHandler FormUpdated;


        private void SupplyAdd_Load(object sender, EventArgs e)
        {
            is_loaded = true;
            supplyBindingSource.DataSource= ctx.Supplies.ToList();
            cartBindingSource.DataSource = ctx.Carts.ToList();
            cbo_Cart.DataSource = cartBindingSource;
            cbo_supply.DataSource = supplyBindingSource;
            if (cartd > 0 && detailId>0)
            {
                Cart s= ctx.Carts.Single(c=>c.Id==cartd);
                CartDetail cd = ctx.CartDetails.Single(d => d.Id == detailId);
                cbo_Cart.SelectedValue = s.Id;
                cbo_supply.SelectedValue = cd.SupplyId;
                txbAmmount.Text = cd.Amount.ToString(); ;
                btn_Add.Enabled = false;
                btn_update.Enabled = true;
            }
            else if (cartd > 0 && detailId <= 0)
            {
                if (ctx.Carts.Single(c => c.Id == cartd) != null)
                {
                    Cart s = ctx.Carts.Single(c => c.Id == cartd);
                    //    CartDetail cd = ctx.CartDetails.Single(d => d.Id == detailId);
                    cbo_Cart.SelectedValue = s.Id;
                    btn_Add.Enabled = true;
                    btn_update.Enabled = false;
                }
            }
            // add this form to window list
            ((Forms.FormMDI)fo).addFormToWindowsList(this);
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            try
            {
                int cartid = (from c in ctx.Carts where c.Name == cbo_Cart.Text select c.Id).First();
                int supplyId = (from s in ctx.Supplies where s.Name == cbo_supply.Text select s.Id).First();
                try
                {

                    CartDetail cd = new CartDetail();
                    cd.CartId = cartid;
                    cd.SupplyId = supplyId;
                    cd.Amount = Double.Parse(txbAmmount.Text);
                    ctx.CartDetails.Add(cd);
                    ctx.SaveChanges();
                    UpdateFormEventArg arg = new UpdateFormEventArg(true);
                    FormUpdated(this, arg);
                }
                catch (Exception)
                {
                    MessageBox.Show("يرجى التأكد من ادخال الكمية واختيار الخيارات الصحيحة");
                } 

            }
            catch (Exception)
            {
                MessageBox.Show("يرجى التأكد من ادخل واختيار الخيارات الصحيحة");
            }
        }

        private void btn_update_Click(object sender, EventArgs e)
        {
            try
            {
                CartDetail s = ctx.CartDetails.Single(c => c.Id == detailId);
                s.Amount = Double.Parse(txbAmmount.Text);
                Supply st = ctx.Supplies.Single(z => z.Name == cbo_supply.Text);
                s.SupplyId = st.Id;
                s.UpdateDate = DateTime.Now;
                s.UpdateUId = 1;
                ctx.SaveChanges();
                //UpdateFormEventArg arg = new UpdateFormEventArg(true);
                //FormUpdated(this, arg);
                this.Close();

            }
            catch (Exception)
            {

                MessageBox.Show("يرجى التأكد من ادخال الكمية واختيار الخيارات الصحيحة");
            }

        }

        private void btn_Dispose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txbAmmount_KeyPress(object sender, KeyPressEventArgs e)
        {
           e.Handled = !Char.IsNumber(e.KeyChar) && e.KeyChar != 8 && e.KeyChar!='.';
            
        }

        private void SupplyAdd_FormClosed(object sender, FormClosedEventArgs e)
        {
            is_loaded = false;
            ((Forms.FormMDI)this.MdiParent).activateWindow(parent);
        }

        private void SupplyAdd_Activated(object sender, EventArgs e)
        {
            // activate this form and add it windows list as activate one
            ((Forms.FormMDI)this.MdiParent).activateWindow(this);
          
           
        }

        

    }
}
